package com.qf08;

import java.util.concurrent.*;

public class Demo4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.创建线程池
        ExecutorService es = Executors.newFixedThreadPool(2);
        //2.提交任务
        Future<Integer> future1 = es.submit(new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                int sum=0;
                for (int i = 0; i <=50 ; i++) {
                    sum+=i;
                }
                System.out.println("1-50计算完毕");
                return sum;
            }
        });
        Future<Integer> future2 = es.submit(new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                int sum=0;
                for (int i = 51; i <=100 ; i++) {
                    sum+=i;
                }
                System.out.println("51-100计算完毕");
                return sum;
            }
        });
        //3。获取结果
        int sum=future1.get()+future2.get();
        //4.关闭线程池
        es.shutdown();
        System.out.println(sum);
    }
}
